package com.kungfuhacking.wristbandpro.chat.presenter;

import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.mqtt.MqttConstant;
import com.kungfuhacking.wristbandpro.mqtt.bean.MessageBean;
import com.kungfuhacking.wristbandpro.utils.AbDateUtil;
import com.kungfuhacking.wristbandpro.utils.Constant;
import com.kungfuhacking.wristbandpro.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Aaron on 17/5/18.
 */

public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.MyViewHolder> {

    private Context mContext;
    private List<MessageBean> chatLists = new ArrayList<>();
    private SharedPreferences sp;
    private String TAG = getClass().getSimpleName();
//    private String talkName;
//    private String talkPic;

    public ChatListAdapter(Context context, List<MessageBean> chatList) {
        this.mContext = context;
        this.chatLists = chatList;
        sp = context.getSharedPreferences(Constant.CONFIG, Context.MODE_PRIVATE);
//        talkName = name;
//        talkPic = pic;
    }

    public void setChatLists(List<MessageBean> chatLists) {
        this.chatLists = chatLists;
    }

    @Override
    public ChatListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChatListAdapter.MyViewHolder(
                LayoutInflater.from(mContext).inflate(R.layout.item_chat_list, parent,
                        false));
    }

    @Override
    public void onBindViewHolder(ChatListAdapter.MyViewHolder holder, int position) {
        setChatMessage(holder, position);
    }

    private void setChatMessage(MyViewHolder holder, int position) {

        if (chatLists == null)
            return;

        //未读消息个数
        String msgCount = "";
        int unReadCount = chatLists.get(position).isRead;

        if (unReadCount > 99) {
            msgCount = "99+";
            holder.tvCount.setBackgroundResource(R.drawable.shape_oval_rectangle_red);
            holder.tvCount.setPadding(DensityUtil.dip2px(mContext, 4), 0, DensityUtil.dip2px(mContext, 4), DensityUtil.dip2px(mContext, 3));
        } else {
            holder.tvCount.setPadding(0, 0, 0, DensityUtil.dip2px(mContext, 3));
            holder.tvCount.setBackgroundResource(R.drawable.shape_circle_red);
            msgCount = unReadCount + "";
        }
        holder.tvCount.setText(msgCount);
        holder.tvCount.setVisibility(unReadCount > 0 ? View.VISIBLE : View.INVISIBLE);
//        holder.tvCount.setTextColor(mContext.getResources().getColor(R.color.textColor_9b));

        switch (chatLists.get(position).messageType) {//内容

            case MqttConstant.MQTTMESSAGETEXT://文字
                holder.tvContent.setText(chatLists.get(position).content);
                holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.textColor_9b));
                break;

//            case MqttConstant.MQTTMESSAGENOTICE:// 创建群 增删群成员
//
//                String content = chatLists.get(position).content;
//                if (sp.getString(MqttConstant.USERID, "").equals(chatLists.get(position).sendId))
//                    content = chatLists.get(position).content.replaceFirst(sp.getString(Constant.NICKNAME, ""), "您");
//                holder.tvContent.setText(content);
//                holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.textColor_9b));
//                break;

            case MqttConstant.MQTTMESSAGEPHOTO://图片
                holder.tvContent.setText("[图片]");
                holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.textColor_9b));
                break;

            case MqttConstant.MQTTMESSAGEVOICE://语音

                holder.tvContent.setText("[语音]");
                holder.tvContent.setTextColor(
                        mContext.getResources().getColor(
                                chatLists.get(position).isListener == MqttConstant.UNLISTENER ?
                                        R.color.red : R.color.textColor_9b)
                );

                break;

            case MqttConstant.MQTTMESSAGETODO://代办
                holder.tvContent.setText("[代办]");
                holder.tvContent.setTextColor(mContext.getResources().getColor(R.color.textColor_9b));
                break;

        }

        //发送时间
        holder.tvTime.setText(
                chatLists.get(position).sendDate == 0 ? "" :
                        AbDateUtil.getChatDate(chatLists.get(position).sendDate));

        if (chatLists.get(position).channelId != null) {
            //系统消息
            if (MqttConstant.START_S.startsWith(chatLists.get(position).channelId)) {
                holder.tvTitle.setText(chatLists.get(position).sendName);//聊天对象的昵称
                holder.sdvHeadPic.setImageResource(R.mipmap.ic_logo_small);
                return;
            }

        }

        //判断是否是群聊
//        if (CommonUtils.isGroupChat(chatLists.get(position).channelId)) {
//          //群聊
        holder.tvTitle.setText(chatLists.get(position).receiveName);//聊天名称
            holder.sdvHeadPic.setImageURI(Uri.parse(
                    TextUtils.isEmpty(chatLists.get(position).receivePic)
                            ? "" : chatLists.get(position).receivePic));

//        } else {//私聊
//
//            //判断发送者是否是本人
//            if (sp.getString(MqttConstant.USERID, "").equals(chatLists.get(position).sendId)) {
//                //是本人发送的
////                Log.d(TAG, "聊天对象的昵称 和 头像 : " + chatLists.get(position).receiveName + "," + chatLists.get(position).receivePic);
//                holder.tvTitle.setText(chatLists.get(position).receiveName);//聊天对象的昵称
////                Log.d(TAG, "设置的头像url:" + chatLists.get(position).receivePic);
//                if (!TextUtils.isEmpty(chatLists.get(position).receivePic))
//                holder.sdvHeadPic.setImageURI(Uri.parse(chatLists.get(position).receivePic));
//            } else {
////                Log.d(TAG, "聊天对象的昵称 和 头像 : " + chatLists.get(position).sendName + "," + chatLists.get(position).sendPic);
//                //不是本人发送的
//                holder.tvTitle.setText(chatLists.get(position).sendName);//聊天对象的昵称
//                if (!TextUtils.isEmpty(chatLists.get(position).sendPic))
//                holder.sdvHeadPic.setImageURI(Uri.parse(chatLists.get(position).sendPic));
//            }
//
//        }
    }

    @Override
    public int getItemCount() {
        if (chatLists == null)
            return 0;
//        Log.e(TAG, "adapter : getItemCount 个数: " + chatLists.size());
        return chatLists.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView sdvHeadPic;
        TextView tvTitle;
        TextView tvCount;
        TextView tvTime;
        TextView tvContent;

        MyViewHolder(View view) {
            super(view);
            sdvHeadPic = (SimpleDraweeView) view.findViewById(R.id.sdvHeadPic);
            tvCount = (TextView) view.findViewById(R.id.tvCount);
            tvTitle = (TextView) view.findViewById(R.id.tvTitle);
            tvContent = (TextView) view.findViewById(R.id.tvContent);
            tvTime = (TextView) view.findViewById(R.id.tvTime);
        }
    }
}